package cn.huijielong.modules.pub.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;
import cn.huijielong.common.utils.RD;
import cn.huijielong.modules.pub.entity.MerchantEntity;
import cn.huijielong.modules.pub.service.MerchantService;
import cn.huijielong.modules.sys.controller.AbstractController;




/**
 * 商户表
 * 
 * @author xys
 * @email xys@gmail.com
 * @date 2019-07-03 22:46:43
 */
@RestController
@RequestMapping("/pub/merchant")
public class MerchantController  extends AbstractController{
	@Autowired
	private MerchantService merchantService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("group:merchant:list")
	public RD list(@RequestParam Map<String, Object> params){
	    logger.info("从{}发送请求，参数：{}",getHeader("referer"),params.toString());
		//查询列表数据
        Query query = Query.createRequest(params);
		Page<MerchantEntity> merchantList = merchantService.queryList(query);		
		PageUtils pageUtil = new PageUtils(merchantList, merchantList.getTotal(), query.getLimit(), query.getPage());
		
		return RD.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{merchantId}")
	@RequiresPermissions("group:merchant:info")
	public RD info(@PathVariable("merchantId") Long merchantId){
		MerchantEntity merchant = merchantService.queryObject(merchantId);
		
		return RD.ok().put("merchant", merchant);
	}
	/**
	 * 信息
	 */
	@RequestMapping("/getUsers/{merchantId}")
	@RequiresPermissions("group:merchant:info")
	public RD getUsers(@PathVariable("merchantId") Long merchantId){
		MerchantEntity merchant = merchantService.queryObject(merchantId);
		
		return RD.ok().put("merchant", merchant);
	}
	/**
	 * 信息
	 */
	@RequestMapping("/getSelectedMerId")
	public RD getSelectedMerId(){
		Long userId=this.getUserId();
		Long merchantId=merchantService.querySelectedMerchantId(userId);
		return RD.ok().put("merchantId", merchantId);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("group:merchant:save")
	public RD save(@RequestBody MerchantEntity merchant){
		merchantService.save(merchant);
		
		return RD.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("group:merchant:update")
	public RD update(@RequestBody MerchantEntity merchant){
		merchantService.update(merchant);
		
		return RD.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("group:merchant:delete")
	public RD delete(@RequestBody Long[] merchantIds){
		merchantService.deleteBatch(merchantIds);
		
		return RD.ok();
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/saveUser")
	@RequiresPermissions("group:merchant:save")
	public RD saveUser(){
		Query q=Query.createRequest();
		merchantService.saveMerchantUser(q);
		return RD.ok();
	}
	/**
	 * 保存
	 */
	@RequestMapping("/removeUser")
	@RequiresPermissions("group:merchant:delete")
	public RD removeUser(){
		Query q=Query.createRequest();
		merchantService.deleteUser(q);
		return RD.ok();
	}
	/**
	 * 删除
	 */
	@RequestMapping("/queryUserByMerId/{merchantId}")
	@RequiresPermissions("group:merchant:info")
	public RD queryUserByMerId(@PathVariable("merchantId") Long merchantId){
		List<Query> list=merchantService.queryUserByMerId(merchantId);
		return RD.ok().setData(list);
	}
	
	
}
